package com.example.demozy.controller;
import com.example.demozy.api.CommonResult;
import com.example.demozy.entity.ConditionSheetCommodity;
import com.example.demozy.service.ConditionSheetCommodityService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 陆智荣
 */
@RestController
public class MallController {
    // **************** 公开变量

    // **************** 私有变量

    @Autowired
    private ConditionSheetCommodityService conditionSheetCommodityService;

    // **************** 继承方法

    // **************** 公开方法
    @GetMapping("/selectAllConditionSheet")
    public CommonResult findRecommendedConditionSheet() {
        if (conditionSheetCommodityService.selectCommodity() != null) {
            return CommonResult
                    .success(conditionSheetCommodityService.selectCommodity(),
                            "success");
        } else {
            return CommonResult.validateFailed();
        }
    }

    @GetMapping("/selectRecommendedConditionSheet")
    public CommonResult selectRecommendedConditionSheet() {
        List recommendedConditionSheetList = new ArrayList();
        for (ConditionSheetCommodity recommendedConditionSheet :
                conditionSheetCommodityService
                .selectCommodity()) {
            if (recommendedConditionSheet.getCommodityPrice() < 700) {
                recommendedConditionSheetList.add(recommendedConditionSheet);
            }
        }
        if (recommendedConditionSheetList.size() == 0){
            return CommonResult.failed("没有满足相对应筛选条件的条件单");
        }else {
            return CommonResult.success(recommendedConditionSheetList, "success");
        }
    }

    @PostMapping("/getConditionSheetCommodity")
    public CommonResult getConditionSheetCommodity(@RequestParam("commodityId")
            String commodityId){
        if (conditionSheetCommodityService.getConditionSheetCommodityById(commodityId)!= null){
            return CommonResult.success(conditionSheetCommodityService.getConditionSheetCommodityById(commodityId),"成功找到返回");
        }else {
            return CommonResult.failed("没有找到相应条件单");
        }
    }

    @PostMapping("/getUserDescriptionByCommodityId")
    public CommonResult getUserDescriptionByCommodityId(@RequestParam("commodityId")
            String commodityId){
        if (conditionSheetCommodityService.getUserDescriptionByCommodityId(commodityId)!=null){
            return CommonResult.success(conditionSheetCommodityService.getUserDescriptionByCommodityId(commodityId),"成功找到并返回");
        }else {
            return CommonResult.failed("没有找到相应用户");
        }
    }

    @PostMapping("/getUserNicknameByCommodityId")
    public CommonResult getUserNicknameByCommodityId(@RequestParam("commodityId")String commodityId){
        if (conditionSheetCommodityService.getUserNicknameByCommodityId(commodityId)!=null){
            return CommonResult.success(conditionSheetCommodityService.getUserNicknameByCommodityId(commodityId),"成功找到并返回");
        }else {
            return CommonResult.failed("没有找到相应用户");
        }
    }

    @PostMapping(value = "/selectConditionSheetOperationData")
    public CommonResult selectConditionSheetOperationData(@Param("commodityId")String commodityId) {
        if (conditionSheetCommodityService.selectConditionSheetOperationDataCommodity(commodityId) != null) {
            return CommonResult
                    .success(conditionSheetCommodityService.selectConditionSheetOperationDataCommodity(commodityId),
                            "success");
        } else {
            return CommonResult.validateFailed();
        }
    }

    @PostMapping("/insertConditionSheetOperationData")
    public CommonResult insertConditionSheetOperationData(@RequestParam("commodityId")String commodityId){
        if (commodityId != null){
            conditionSheetCommodityService.insertConditionSheetOperationData(commodityId);
            return CommonResult.success("返回成功");
        } else {
            return CommonResult.validateFailed();
        }
    }

    @PostMapping("/getUserIdByCommodityId")
    public CommonResult getUserIdByCommodityId(@RequestParam("commodityId")String commodityId){
        if (conditionSheetCommodityService.getUserIdByCommodityId(commodityId)!=null){
            return CommonResult.success(conditionSheetCommodityService.getUserIdByCommodityId(commodityId),"成功找到并返回");
        }else {
            return CommonResult.failed("没有找到相应用户");
        }
    }

    @PostMapping("/purchaseCommodityByCommodityId")
    public CommonResult purchaseCommodityByCommodityId(@RequestParam("userId")String userId, @RequestParam("commodityId")
            String commodityId, @RequestParam("goodsPrice")double goodsPrice){
        if (conditionSheetCommodityService.selectOrderCommodityId(userId, commodityId).size()==0 && conditionSheetCommodityService.selectUserBalance(userId) != 0 && conditionSheetCommodityService.getConditionSheetCommodityById(commodityId).getCommodityPrice() <= conditionSheetCommodityService.selectUserBalance(userId)){
            conditionSheetCommodityService.purchaseCommodityByCommodityId(commodityId, userId);
            conditionSheetCommodityService.insertOrder(userId, commodityId, goodsPrice);
            conditionSheetCommodityService.updateconditionSheetRegistration(commodityId);
            return CommonResult.success("1");
        }else if (conditionSheetCommodityService.getConditionSheetCommodityById(commodityId).getCommodityPrice() >= conditionSheetCommodityService.selectUserBalance(userId)){
            return CommonResult.failed("2");
        }
        return CommonResult.failed("0");
    }


    @GetMapping(value = "/getConditionSheetsWhereOnStateIsOne")
    public CommonResult getConditionSheetsByOnState(){
        return CommonResult.success(conditionSheetCommodityService.getConditionSheetsWhereOnStateIsOne());
    }

    @PostMapping(value = "/auditSuccess")
    public CommonResult auditSuccess(@Param("conditionSheetId") String conditionSheetId, @Param("commodityName")String commodityName,
            @Param("commodityDescription")String commodityDescription, @Param("commodityPrice")double commodityPrice){
        conditionSheetCommodityService.updateConditionSheetOnStateToTwo(conditionSheetId);
        conditionSheetCommodityService.deleteConditionSheetRecord(conditionSheetId);
        conditionSheetCommodityService.insertConditionSheetIntoCommodity(conditionSheetId,commodityName,commodityDescription,commodityPrice);
        conditionSheetCommodityService.insertNewConditionSheetOperationData();
        return CommonResult.success("条件单过审成功");
    }

    @PostMapping(value = "/auditFail")
    public CommonResult auditFail(@Param("conditionSheetId")String conditionSheetId){
        conditionSheetCommodityService.updateConditionSheetOnStateToZero(conditionSheetId);
        conditionSheetCommodityService.deleteConditionSheetRecord(conditionSheetId);
        return CommonResult.success("条件单驳回成功");
    }

    @PostMapping(value = "/selectUserBalance")
    public CommonResult selectUserBalance(@Param("userId")String userId){
        return CommonResult.success(conditionSheetCommodityService.selectUserBalance(userId));
    }

    @PostMapping(value = "/selectAllOrders")
    public CommonResult selectAllOrders(@Param("userId")String userId){
        return CommonResult.success(conditionSheetCommodityService.selectAllOrders(userId));
    }
}
